//
//  TSRecieveMsgTableView.m
//  chart
//
//  Created by xu on 15/7/26.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSRecieveMsgTableView.h"
#import "TSMessageListCell.h"
#import "TSRecieverListModel.h"
#import "TSMsgEditTableViewController.h"


@implementation TSRecieveMsgTableView{
    TSReceiveMsgListModel *msgListModel;
}

@synthesize dataArray;

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self=[super initWithFrame:frame style:style]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hasLogined:) name:TSNotification_HasLogined object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hasLogined:) name:TSNotification_MessageChangeActionSucceeded object:nil];
        
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableFooterView = [UIView new];
        
        self.dataSource = self;
        self.delegate = self;
        
        msgListModel = [[TSReceiveMsgListModel alloc] init];
    }
    
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
//        UITableViewCell *default_cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
//        default_cell.textLabel.text = @"时间         标题";
//        default_cell.detailTextLabel.text = @"部门-用户名    状态";
        
        static NSString *cell_head_identify = @"cellIdentify_head";
        TSMessageListCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_head_identify];
        if (!cell) {
            cell = [[TSMessageListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_head_identify isHeader:true];
        }
        return cell;
    }
    else{
        static NSString *cell_identify = @"cellIdentify";
        TSMessageListCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];
        if (!cell) {
            cell = [[TSMessageListCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
        }
        
        if (dataArray) {
            TSReceiveMsgModel *model = [dataArray objectAtIndex:indexPath.row-1];
            cell.isSendNotReceive = NO;
            [cell setModel:model];
        }
    
        return cell;
    }
    
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray) {
        return [dataArray count] + 1;
    }
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TSMsgEditTableViewController *edit_tvc = [[TSMsgEditTableViewController alloc] init];
    [edit_tvc setHidesBottomBarWhenPushed:YES];
    
    if (indexPath.row == 0) {
        return;
    }
    
    if (dataArray) {
        TSReceiveMsgModel *model = [dataArray objectAtIndex:indexPath.row-1];
        edit_tvc.msgId = model.msgId;
    }
    
    [[self getViewController].navigationController pushViewController:edit_tvc animated:YES];
}


#pragma mark - requestData

- (void)hasLogined:(NSNotification *)noti
{
    [self headerRequestData];
}

- (void)headerRequestData
{
    WS(weakSelf);
    self.pageIndex = 0;
    msgListModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize)};
    [msgListModel fetchWithPath:kRequestUrl_SendMessage_outReceive_message completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (msgListModel.list.count > 0) {
            strongSelf.footer.hidden = NO;
            strongSelf->dataArray = msgListModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

- (void)footerRequestData
{
    WS(weakSelf);
    self.pageIndex++;
    msgListModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize)};
    [msgListModel fetchWithPath:kRequestUrl_SendMessage_outReceive_message completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        
        if (msgListModel.list.count == 0) {
            strongSelf.footer.hidden = YES;
        }
        else{
            strongSelf->dataArray = msgListModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}



@end
